package com.novel.authorization.staff.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.novel.authorization.staff.entity.NfTenant;
import com.novel.authorization.staff.mapper.NfTenantMapper;
import com.novel.authorization.staff.service.NfTenantService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 租户表
 *
 * @author 樊温军
 * @date Wed May 07 10:50:37 CST 2025
 */
@Slf4j
@Service
@AllArgsConstructor
public class NfTenantServiceImpl extends ServiceImpl<NfTenantMapper, NfTenant> implements NfTenantService {

    @Override
    public List<NfTenant> getNfTenantList(NfTenant nfTenant) {
        return baseMapper.getList(nfTenant);
    }

    @Override
    public IPage<NfTenant> getPage(Page<NfTenant> page, NfTenant nfTenant) {
        return baseMapper.getPage(page, nfTenant);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveOneNfTenant(NfTenant nfTenant) {
        this.saveOrUpdate(nfTenant);
        return nfTenant.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delById(Long id) {
        baseMapper.delete(Wrappers.<NfTenant>query().lambda().in(NfTenant::getId, id));
    }

    @Override
    public List<NfTenant> getNormalTenant() {
        return baseMapper.selectList(Wrappers.<NfTenant>query().lambda()
                .eq(NfTenant::getStatus, "1")
                .and(wrapper -> wrapper.ge(NfTenant::getExpireTime, LocalDateTime.now()).or().isNull(NfTenant::getExpireTime))
                .and(wrapper -> wrapper.le(NfTenant::getEffectTime, LocalDateTime.now()).or().isNull(NfTenant::getEffectTime)));
    }

    @Override
    public NfTenant queryById(Long id) {
        return baseMapper.selectById(id);
    }
}
